<?php
namespace App\Services\Mobile\Auth;

use App\Models\Agent\Agent;
use App\Models\Register\Register;
use App\Models\User\Users;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Support\Facades\DB;

class AuthLogic
{
    // 代理登录
    public function login($data)
    {
        $condition['user_name'] = $data['user_name'];
        $condition['password'] = $data['password'];

        $res = Agent::where($condition)->first();
        if ($res) {
            if ($res['status'] == 1) {
                return auto_failure([], '该用户已注销');
            }
            if ($res['status'] == 3 && $res['dead_time'] > time()) {
                return auto_failure([], '该用户已停用');
            }
            if ($res['status'] == 3 && $res['dead_time'] <= time()) {
                $updateData['status'] = 2;
                $updateData['dead_time'] = 0;
                $rst = Agent::where('id', $res['id'])->update($updateData);
                if ($rst) {
                    return auto_return($res, '用户匹配成功');
                }
                return auto_failure([], '用户匹配异常，请重试!');
            }
            return auto_return($res, '用户匹配成功');
        }
        return auto_failure($res, '未匹配到该用户');
    }

    // 注册音乐人
    public function register($data)
    {
        unset($data['token']);
        unset($data['code']);
        $data['status'] =2;
        $data['user_name'] = $data['phone'];
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['signed_time'] = date('Y-m-d');
        $data['expired_time'] = strtotime("+".$data['sign_time']." year");
        $data['password'] = (new BcryptHasher())->make($data['phone']);
        DB::beginTransaction();
        try {
            $res = Register::insertGetId($data);
            $usersData = [
                'info_id' => $res,
                'uuid' => rand_string(32)
            ];
            Users::insert($usersData);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return auto_failure($e->getMessage());
        }
        if ($res) {
            return auto_return($res, "成功注册");
        }
        return auto_failure($res, "失败");
    }

    // 验证身份
    public function checkIdentity($data)
    {
        $condition['id'] = $data['id'];
        $condition['password'] = md5($data['password']);

        $res = Agent::where($condition)->first();
        if ($res) {
            return auto_return($res, '成功');
        }
        return auto_failure($res, '密码匹配失败');
    }

    // 更新密码
    public function updateInfo($data)
    {
        $condition['id'] = $data['id'];
        $condition['password'] = md5($data['password']);

        $res = Agent::where('id', $condition['id'])->update($condition);
        if ($res) {
            return auto_return($res, '修改成功');
        }
        return auto_failure($res, '修改失败');
    }

    // 验证身份
    public function checkPhone($data)
    {
        $res = Agent::where('phone', $data['phone'])->first();
        if ($res) {
            return auto_return($res, '成功');
        }
        return auto_failure($res, '手机匹配失败');
    }
}